#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/19 17:14
@desc 提取城市的POI点信息并将其保存至MongoDB数据库
"""
import urllib2
import json
from pymongo import MongoClient

left_bottom = [116.282387, 39.835862]  # 设置区域左下角坐标（百度坐标系）
right_top = [116.497405, 39.996569]  # 设置区域右上角坐标（百度坐标系）
part_n = 2  # 设置区域网格（2*2）
client = MongoClient("mongodb://lyndon:pass123@cluster0-shard-00-00-1nouk.mongodb.net:27017,cluster0-shard-00-01-1nouk.mongodb.net:27017,cluster0-shard-00-02-1nouk.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true")
db = client.im
col = db.latitude  # 连接集合
url0 = 'http://api.map.baidu.com/place/v2/search?'
x_item = (right_top[0] - left_bottom[0]) / part_n
y_item = (right_top[1] - left_bottom[1]) / part_n
query = '饭店'  # 搜索关键词设置
ak = 'vsZacTI74G8n6lG1b47sSwKjhxqey2MD'  # 百度地图api信令
n = 0  # 切片计数器
for i in range(part_n):
    for j in range(part_n):
        left_bottom_part = [left_bottom[0] + i * x_item, left_bottom[1] + j * y_item]  # 切片的左下角坐标
    right_top_part = [right_top[0] + i * x_item, right_top[1] + j * y_item]  # 切片的右上角坐标
    for k in range(30):
        url = url0 + 'query=' + query + '&page_size=20&page_num=' + str(k) + '&scope=1&bounds=' + str(
            left_bottom_part[1]) + ',' + str(left_bottom_part[0]) + ',' + str(right_top_part[1]) + ',' + str(
            right_top_part[0]) + '&output=json&ak=' + ak
        data = urllib2.urlopen(url)
        hjson = json.loads(data.read())
        if hjson['message'] == 'ok':
            results = hjson['results']
        for m in range(len(results)):  # 提取返回的结果
            col.insert_one(results[m])
            crwal_data = dict(results[m])
            print crwal_data.get('province'), crwal_data.get('city'), crwal_data.get('name'), crwal_data.get('area'), crwal_data.get('location'), crwal_data.get('address')
    n += 1
    print '第', str(n), '个切片入库成功'
